// pages/goods_list/index.js
import { request } from '../../request/index'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    tabs: [
      {
        id: 0,
        value: "综合",
        isActive: true,
      },
      {
        id: 1,
        value: "销量",
        isActive: false,
      },
      {
        id: 2,
        value: "价格",
        isActive: false,
      },
    ],
    goodsList:[],
    // 商品数量
    num:0
  },
  QueryParams: {
    query: "",
    cid: "",
    pagenum: 1,
    pagesize: 10,
  },
  onLoad: function (options) {
    this.QueryParams.cid = options.cid
    this.getGoodsList()
  },
  // 获取商品数据
  getGoodsList(){
    request({url:'/goods/search',data:this.QueryParams})
      .then(result=>{
        let goodsList = result.data.message.goods
        this.num = result.data.message.total
        this.setData({
          goodsList
        })
        wx.stopPullDownRefresh()
      })
  },
  // 导航栏点击事件
  handleTabsItemChange(e) {
    // 获取子组件传递过来的值
    let { index } = e.detail;
    // 获取 data 中数据
    let { tabs } = this.data;
    // 修改 tabs 中 isActive 的值
    tabs.forEach((v, i) =>
      index === i ? (v.isActive = true) : (v.isActive = false)
    );
    this.setData({
      tabs,
    });
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.QueryParams.pagesize = 10
    this.setData({
      num:0,
      goodsList:[]
    })
    this.getGoodsList()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    // 获取商品下一页数据
    let pagesize = this.QueryParams.pagesize + 10
    // 判断商品数量是否大于 goodsList 商品数量
    if(this.num > this.data.goodsList.length){
      this.QueryParams.pagesize = pagesize
      this.getGoodsList()
    }else{
      wx.showToast({title:'到底了'})
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
});
